package cn.edu.fzu.delivery.service.impl;

import cn.edu.fzu.delivery.domain.entity.Courier;
import cn.edu.fzu.delivery.domain.entity.Customer;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.CourierStatusEnum;
import cn.edu.fzu.delivery.domain.enums.NetworkCourierStatusEnum;
import cn.edu.fzu.delivery.exception.ServiceException;
import cn.edu.fzu.delivery.handler.DeliveryHandler;
import cn.edu.fzu.delivery.handler.DeliverySession;
import cn.edu.fzu.delivery.helper.DingDingRobot;
import cn.edu.fzu.delivery.mapper.NetworkCourierMapper;
import cn.edu.fzu.delivery.service.CourierDeliveryService;
import cn.edu.fzu.delivery.service.CourierService;
import cn.edu.fzu.delivery.service.NetworkAdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class NetworkAdminServiceImpl implements NetworkAdminService {

    @Resource
    private CourierDeliveryService courierDeliveryService;

    @Resource
    private CourierService         courierService;

    @Resource
    private NetworkCourierMapper   networkCourierMapper;

    @Resource
    private List<DeliveryHandler>  deliveryHandlers;

    @Override
    public List<Courier> listAllCouriers(Long networkId, String courierId) {
        List<String> courierIds = courierDeliveryService.listCourierIdsByNetWork(networkId);
        if (StringUtils.isNotBlank(courierId)) {
            return courierIds.contains(courierId) ? Collections.singletonList(courierService.getByCourierId(courierId))
                    : null;
        }
        if(CollectionUtils.isEmpty(courierIds)){
            return Collections.emptyList();
        }
        return courierService.listByCourierIds(courierIds);
    }


    @Override
    public List<Courier> listDeliveryCouriers(Long networkId, String courierId) {
        List<String> courierIds = networkCourierMapper.getCouriersByNetworkId(networkId);
        if (CollectionUtils.isEmpty(courierIds)) {
            return new ArrayList<>(0);
        }
        List<Courier> list = null;
        if (StringUtils.isNotBlank(courierId)) {
            Courier courier = courierService.getByCourierId(courierId);
            List<Courier> single = new ArrayList<>(1);
            single.add(courier);
            list = courierIds.contains(courierId) ? single : null;
        } else {
            list = courierService.listByCourierIds(courierIds);
        }
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        //去除掉已经派送的快递
        list.removeIf(courier -> courier.getLogistics().getStatus() == NetworkCourierStatusEnum.LEAVE);
        return list;
    }


    @Override
    public void delivery(SysUser networkAdmin, String courierId) {
        Courier courier = courierService.getByCourierId(courierId);

        if (courier.getLogistics().getStatus() == NetworkCourierStatusEnum.LEAVE) {
            throw new ServiceException("该快递已经被派送");
        }

        DeliverySession session = DeliverySession.build(courier);
        for (DeliveryHandler handler : deliveryHandlers) {
            if (!handler.process(session)) {
                List<String> items = new ArrayList<>();
                items.add("快递单号：" + courierId);
                items.add("失败原因：" + session.getError().getDesc());
                DingDingRobot.delivery().send("派件失败通知", "请及时处理该快递", items,
                        Collections.singletonList(networkAdmin.getPhone()), null);
                return;
            }
        }

        if (Objects.equals(courier.getSender().getNetwork().getId(), networkAdmin.getNetwork().getId())) {
            //当前网点为寄件网点，则状态修改为已发货
            courierService.update(courierId, null, CourierStatusEnum.SEND, null, null, true);
        }

    }


    @Override
    public void addCourierToNetwork(Long networkId, String courierId) {
        networkCourierMapper.insert(networkId, courierId);
    }


    @Override
    public void delCourierFromNetwork(Long networkId, String courierId) {
        networkCourierMapper.delete(networkId, courierId);
    }
}
